package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageOtherIndexBean;

import java.util.List;

/**
 * 作者：wsy on 2017/12/7 12:05
 * <p>
 * 邮箱：445979770@qq.com
 */

public class OtherOutStorageMoveListAdapter extends BaseAdapter {

    private Context mContext;
    private List<OutStorageOtherIndexBean.BodyBean.MoveListBean> mMoveList;
    private LayoutInflater inflater;
    private MoveListAdapterListener mMoveListAdapterListener;

    public OtherOutStorageMoveListAdapter(Context context, List<OutStorageOtherIndexBean.BodyBean.MoveListBean> moveList, MoveListAdapterListener moveListAdapterListener) {
        mContext = context;
        mMoveList = moveList;
        mMoveListAdapterListener = moveListAdapterListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mMoveList.size();
    }

    @Override
    public Object getItem(int position) {
        return mMoveList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OutStorageOtherIndexBean.BodyBean.MoveListBean moveItem = mMoveList.get(position);
        OtherOutStorageMoveListAdapter.ViewHolder viewHolder;
        if (convertView == null || (convertView != null && convertView.getTag() == null)) {
            convertView = inflater.inflate(R.layout.view_in_storage_select, null);
            viewHolder = new OtherOutStorageMoveListAdapter.ViewHolder();
            //初始化控件
            initView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (OtherOutStorageMoveListAdapter.ViewHolder) convertView.getTag();
        }
        //初始化数据
        initData(mContext, viewHolder, convertView, moveItem);
        return convertView;
    }

    private void initView(OtherOutStorageMoveListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.text = (TextView) view.findViewById(R.id.text);
        viewHolder.value = (TextView) view.findViewById(R.id.value);
    }

    private void initData(Context context, OtherOutStorageMoveListAdapter.ViewHolder viewHolder, View view, OutStorageOtherIndexBean.BodyBean.MoveListBean moveItem) {
        viewHolder.text.setText(moveItem.getMove_type_code() + " - " + moveItem.getMove_type_name() + (TextUtils.isEmpty(moveItem.getSpec_stock()) ? "" : " - " + moveItem.getSpec_stock()));
        viewHolder.value.setText(moveItem.getMove_type_code());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                mMoveListAdapterListener.onSelect(moveItem.getMove_type_id(), moveItem.getMove_type_code(), moveItem.getMove_type_name(), moveItem.getSpec_stock());
            }
        });
    }

    private class ViewHolder {
        private TextView text, value;
    }

    public static abstract class MoveListAdapterListener {
        public abstract void onSelect(String id, String code, String name, String specialInventory);
    }
}
